package com.hzhr.iot.device.service;

import com.hzhr.iot.core.annotation.ApiImplicitInternalParams;
import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.device.constant.Device_ErrorsEnum;
import com.hzhr.iot.device.dao.mapper.custom.PcCollectDeviceIotDeviceMapper;
import com.hzhr.iot.device.entity.CollectDeviceEntity;
import com.hzhr.iot.device.entity.IotDeviceEntity;
import com.hzhr.iot.device.entity.ext.IotDeviceEntityExt;
import com.hzhr.iot.device.model.regulatorDevice.RegulatorDeviceGetModel;
import com.hzhr.iot.device.model.regulatorDevice_implRecord.RegulatorDevice_ImplRecordModel;
import com.hzhr.iot.device.model.regulatorDevice_monitor.RegulatorDevice_MonitorGetModel;
import com.hzhr.iot.device.model.regulatorDevice_monitor.RegulatorDevice_MonitorModel;
import com.hzhr.iot.device.service.base.BaseService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class RegulatorDevice_MonitorService extends BaseService {
    @Autowired
    PcCollectDeviceIotDeviceMapper pcCollectDeviceIotDeviceMapper;


    //region javadoc注释 : get
    /**
     * 查询调压设备下的监控设备 <br/><br/>
     * 步骤 : <br/>
     * 1. 查询调压设备 : <br/>
     * 查询COLLECT_DEVICE对象regulator_1， <br/>
     * 查询规则如下： <br/>
     *           (1) COLLECT_DEVICE.ID == 路径参数regulatorDeviceId; <br/>
     *           (2) COLLECT_DEVICE.IS_VALID == '1'； <br/>
     * 如果regulator_1不存在，抛出异常201； <br/>
     * 2. 查询调压设备的下的监控设备 : <br/>
     * 查询IOT_DEVICE对象集合monitors_1及总数, <br/>
     * 查询规则如下： <br/>
     *           (1) IOT_DEVICE.COLLECT_DEVICE_ID == regulator_1.ID; <br/>
     *           (2) IOT_DEVICE.IS_VALID == '1'; <br/>
     * 3. 返回信息 <br/>
     * 根据条件参数、monitors_1及总数生成RegulatorDevice_MonitorGetModel对象result并返回， <br/>
     * RegulatorDevice_MonitorModel与IOT_DEVICE的对应关系如下： <br/>
     *           (1) RegulatorDevice_MonitorModel.id => IOT_DEVICE.ID; <br/>
     *           (2) RegulatorDevice_MonitorModel.code => IOT_DEVICE.CODE; <br/>
     *           (3) RegulatorDevice_MonitorModel.des => IOT_DEVICE.DES; <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/>
     * _show : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 展示形式 one:展示一个,list:展示列表,null(空值):不展示;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _show_total : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示总数;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/><br/>
     * @param regulatorDeviceId 调压设备编号 <br/><br/>
     * @throws ServiceException <br/>
     * @see RegulatorDevice_MonitorGetModel result <br/>
     */ 
    //endregion
    //region 说明 : get
    //查询调压设备下的监控设备
    //步骤 :
    //1. 查询调压设备 :
    //查询COLLECT_DEVICE对象regulator_1，
    //查询规则如下：
    //          (1) COLLECT_DEVICE.ID == 路径参数regulatorDeviceId;
    //          (2) COLLECT_DEVICE.IS_VALID == '1'；
    //如果regulator_1不存在，抛出异常201；
    //2. 查询调压设备的下的监控设备 :
    //查询IOT_DEVICE对象集合monitors_1及总数,
    //查询规则如下：
    //          (1) IOT_DEVICE.COLLECT_DEVICE_ID == regulator_1.ID;
    //          (2) IOT_DEVICE.IS_VALID == '1';
    //3. 返回信息
    //根据条件参数、monitors_1及总数生成RegulatorDevice_MonitorGetModel对象result并返回，
    //RegulatorDevice_MonitorModel与IOT_DEVICE的对应关系如下：
    //          (1) RegulatorDevice_MonitorModel.id => IOT_DEVICE.ID;
    //          (2) RegulatorDevice_MonitorModel.code => IOT_DEVICE.CODE;
    //          (3) RegulatorDevice_MonitorModel.des => IOT_DEVICE.DES;
    //endregion
    //region 参数 : get
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),

            @ApiImplicitParam(name = "regulatorDeviceId", value = "调压设备编号",required = true, dataType = "int", paramType = "path"),

            @ApiImplicitParam(name = "show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "show_total", value = "是否展示总数", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),

            @ApiImplicitParam(name = "_show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_show_total", value = "是否展示总数", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
    })
    //endregion
    //region 请求 : get
    @RequestMapping(value = "/regulator-devices/{regulatorDeviceId}/monitors", method = RequestMethod.GET)
    //endregion
    public RegulatorDevice_MonitorGetModel get(Map<String, String> header, Integer regulatorDeviceId, Map<String, Object> query) throws ServiceException {

        String _show = HzhrTypeUtil.object2string(query.get("_show"));
        Boolean _show_total = HzhrTypeUtil.object2boolean(query.get("_show_total"));

        //1. 查询调压设备
        CollectDeviceEntity regulator_1 = pcCollectDeviceIotDeviceMapper.selectIdAndIsValid(regulatorDeviceId);
        if (regulator_1 == null){
            throw new ServiceException(Device_ErrorsEnum.REGULATORDEVICE_MONITOR_ALARMDATASERVICE_GET_201);
        }

        //2. 查询调压设备的下的监控设备
        List<IotDeviceEntity> monitors_1 = null;
        Integer regulatorId_1 = regulator_1.getId();
        if (_show != null) {
         monitors_1 = pcCollectDeviceIotDeviceMapper.selectIotCollectDeviceIdAndValid(regulatorId_1);
        }
        if (monitors_1 == null){
            throw new ServiceException(Device_ErrorsEnum.REGULATORDEVICE_MONITOR_ALARMDATASERVICE_GET_202);
        }

        Integer Total = null;
        if (_show_total){
            Total = pcCollectDeviceIotDeviceMapper.selectCountIotCollectDeviceIdAndValid(regulatorId_1);
        }

        //3. 返回信息
        //根据条件参数、monitors_1及总数生成RegulatorDevice_MonitorGetModel对象result并返回，
        //RegulatorDevice_MonitorModel与IOT_DEVICE的对应关系如下：
        //          (1) RegulatorDevice_MonitorModel.id => IOT_DEVICE.ID;
        //          (2) RegulatorDevice_MonitorModel.code => IOT_DEVICE.CODE;
        //          (3) RegulatorDevice_MonitorModel.des => IOT_DEVICE.DES;
        //endregion
        RegulatorDevice_MonitorGetModel result = new RegulatorDevice_MonitorGetModel();

        if ("one".equals(_show)) {
            IotDeviceEntity iotDeviceEntityExt = monitors_1.get(0);
            RegulatorDevice_MonitorModel regulatorDevice_monitorModel = new RegulatorDevice_MonitorModel();
            regulatorDevice_monitorModel.setId(iotDeviceEntityExt.getId());
            regulatorDevice_monitorModel.setCode(iotDeviceEntityExt.getCode());
            regulatorDevice_monitorModel.setDes(iotDeviceEntityExt.getDes());
            regulatorDevice_monitorModel.setCategoryCode(iotDeviceEntityExt.getCategoryCode());
            result.setModel(regulatorDevice_monitorModel);
        }
        if ("list".equals(_show)) {
            List<RegulatorDevice_MonitorModel> regulatorDevice_monitorModels = new ArrayList<>();
            for (IotDeviceEntity iotDeviceEntityExt : monitors_1) {
                RegulatorDevice_MonitorModel regulatorDevice_monitorModel = new RegulatorDevice_MonitorModel();
                regulatorDevice_monitorModel.setId(iotDeviceEntityExt.getId());
                regulatorDevice_monitorModel.setCode(iotDeviceEntityExt.getCode());
                regulatorDevice_monitorModel.setDes(iotDeviceEntityExt.getDes());
                regulatorDevice_monitorModel.setCategoryCode(iotDeviceEntityExt.getCategoryCode());
                regulatorDevice_monitorModels.add(regulatorDevice_monitorModel);
                result.setModelList(regulatorDevice_monitorModels);
            }
        }

        result.setTotal(Total);
        return result;
    }
}

